<!--  -->
<template>
  <div class="clumb">首页 /</div>
  <hr />
  <div class="search">
    <div class="top">
      <span>筛选搜索</span>
      <div>
        <button>重置</button>
        <button>查询搜索</button>
      </div>
    </div>
    <div class="bottom">
      账号/姓名
      <input type="text" />
    </div>
  </div>
  <div class="addlist">
    <span>数据列表</span>
    <button @click="add">添加</button>
  </div>
  <div class="table">
    <el-table
      :data="tableData"
      :style="{ width: '100%' }"
      border
      :cell-style="{ 'text-align': 'center' }"
      :header-cell-style="{
        'text-align': 'center',
      }"
    >
      <el-table-column prop="id" label="编号" />
      <el-table-column prop="username" label="账号" />
      <el-table-column prop="nickName" label="姓名" />
      <el-table-column prop="email" label="邮箱" />
      <el-table-column label="添加时间">
        <template #default="{ row }">
          {{ formatDate(row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="最后登录">
        <template #default="{ row }">
          {{ formatDate(row.loginTime) }}
        </template>
      </el-table-column>
      <el-table-column label="是否启用">
        <template #default="{ row }">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            v-model="row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button 
            size="small"
            type="success"
            @click="allocRoleInfo(row)"
          >分配角色</el-button>
          <el-button
            size="small"
            type="success"
            @click="editAdminInfo(row)"
            >编辑</el-button>
            <!--  -->
        </template>
      </el-table-column>
    </el-table>
  </div>
  <Dialog 
  :visible="editAdminVisible" 
  @close="closeAdminInfo"
  :form-data="editAdminData"
   />
   <Create 
   :visible="allocRolesVisible"
   @close="closeAllocRole"
   :admin-id="adminId"
    />
  <div class="pages">
    <el-pagination
      v-model:currentPage="pager.currentPage"
      v-model:page-size="pager.pageSize"
      :page-sizes="[5, 10, 20, 50, 100]"
      :small="pager.small"
      :disabled="pager.disabled"
      :background="pager.background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pager.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { umsAdmin } from "../../../request/http";
import dayjs from "dayjs";
import Dialog from "./components/EditAdmin.vue";
import Create from "./components/AllocRoles.vue";


let tableData = ref([] as {}[]);

// 分页
const pager = reactive({
  currentPage: 1,
  pageSize: 10,
  small: true,
  disabled: false,
  background: false,
  total: 0,
});
function handleSizeChange() {
  fetchData(pager.currentPage, pager.pageSize);
}
function handleCurrentChange() {
  fetchData(pager.currentPage, pager.pageSize);
}
// 请求数据
async function fetchData(pageNum: number = 1, pageSize: number = 10) {
  const adminListRes = await umsAdmin.getAdminList({
    pageNum,
    pageSize,
  });
  if (adminListRes.code === 200) {
    tableData.value = adminListRes.data.list;
    pager.total = adminListRes.data.total;
  }
}
fetchData(pager.currentPage, pager.pageSize);

// 时间处理
function formatDate(time?: string) {
  if (!time) return "";
  return dayjs(time).format("YYYY-MM-DD HH:mm:ss");
}
// 操作
const editAdminVisible = ref(false);
const editAdminData = ref({} as AdminResDataObj);
function editAdminInfo(row:AdminResDataObj){
  editAdminVisible.value = true;
  editAdminData.value = row;
}
function closeAdminInfo(r?:"reload"){
  editAdminVisible.value = false;
  if(r === "reload"){
    // 更新表格数据
    fetchData();
  }
}
// 分配角色
const allocRolesVisible = ref(false);
const adminId = ref();
function allocRoleInfo(row: AdminResDataObj) {
  allocRolesVisible.value = true;
  adminId.value = row.id;
}
function closeAllocRole() {
  allocRolesVisible.value = false;
}

// 添加
function add(){
  editAdminVisible.value = true;
  editAdminData.value = {} as AdminResDataObj;
}
</script>
<style lang="less" scoped>
.clumb {
  height: 40px;
  line-height: 40px;
  padding-left: 20px;
}
.search {
  border: 1px solid #333;
  height: 120px;
  margin: 5px 10px;
  .top {
    margin: 20px;
    display: flex;
    justify-content: space-between;
    button {
      margin-right: 10px;
    }
  }
  .bottom {
    margin-left: 20px;
  }
}
.addlist {
  border: 1px solid #333;
  margin: 5px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  span {
    margin-left: 20px;
  }
  button {
    margin-right: 30px;
  }
}
.table {
  margin: 10px;
}
</style>
